﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;
using SW.Framework.Repository;
using SW.Framework.UnitOfWork;
using SW.Framework.Entity;
using System.Data.Entity;
namespace SW.Framework.EF
{
   public  class EFRepository<TEntity>:IRepository<TEntity> where TEntity:Entity.Entity
    {
        public EFUnitOfWork<TEntity> unitofwork = new EFUnitOfWork<TEntity>();
        #region 属性

        /// <summary>
        /// 实体集合
        /// </summary>
        public IEnumerable<TEntity> Entities
        {
            get
            {
                return  unitofwork.context.Set<TEntity>().AsEnumerable();
            }
        } 
        /// <summary>
        /// 实体集合总数
        /// </summary>
       public  int Count { get { return unitofwork.context.Set<TEntity>().Count(); }  }
        #region 公共方法
        /// <summary>
        /// 检查记录是否存在
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
       public  bool IsContains(Expression<Func<TEntity, bool>> expression)
        {
            return unitofwork.context.Set<TEntity>().Count(expression) > 0;
        }
        #endregion
        #endregion
        #region 新增方法
        /// <summary>
        /// 单项新增
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
       public  int Insert(TEntity entity)
        {
            unitofwork.RegisterNew(entity);
            return unitofwork.Commit();
        }
        public int Insert(TEntity entity,bool IsSave)
        {
            unitofwork.RegisterNew(entity);
            return IsSave? unitofwork.Commit():0;
        }
        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="entities">实体集合</param>
        /// <returns></returns>
       public int Insert(IEnumerable<TEntity> entities)
        {   foreach (var obj in entities)
            {
                unitofwork.RegisterNew(obj);
            }
            return unitofwork.Commit();
        }
       public int Insert(IEnumerable<TEntity> entities,bool IsSave)
        {
            foreach (var obj in entities)
            {
                unitofwork.RegisterNew(obj);
            }
            return IsSave? unitofwork.Commit():0;
        }
        #endregion
        #region 删除方法
        /// <summary>
        /// 单项删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
       public int Delete(TEntity entity)
        {
            unitofwork.RegisterDeleted(entity);
            return unitofwork.Commit();
        }
       public int Delete(TEntity entity,bool IsSave)
        {
            unitofwork.RegisterDeleted(entity);
            return IsSave? unitofwork.Commit():0;
        }
        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
       public int Delete(object ID)
        {
            var obj = unitofwork.context.Set<TEntity>().Find(ID);
            if (obj == null)
            {
                return 0;
            }
            unitofwork.RegisterDeleted(obj);
            return unitofwork.Commit();
        }
       public int Delete(object ID,bool IsSave)
        {
            var obj = unitofwork.context.Set<TEntity>().Find(ID);
            if (obj == null)
            {
                return 0;
            }
            unitofwork.RegisterDeleted(obj);
            return IsSave? unitofwork.Commit():0;
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="entities">实体集合</param>
        /// <returns></returns>
       public int Delete(IEnumerable<TEntity> entities)
        {
            foreach (var entity in entities)
            {
                unitofwork.RegisterDeleted(entity);
            }
            return unitofwork.Commit();
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="expression">删除Lamada表达式</param>
        /// <returns></returns>
       public int Delete(Expression<Func<TEntity, bool>> expression)
        {
            Func<TEntity, bool> lamada = expression.Compile();
            var lstEntity = unitofwork.context.Set<TEntity>().Where(lamada);
            foreach (var entity in lstEntity)
            {
                unitofwork.RegisterDeleted(entity);
            }
            return unitofwork.Commit();
           
        }
       public  int Delete(Expression<Func<TEntity, bool>> expression,bool IsSave)
        {
            Func<TEntity, bool> lamada = expression.Compile();
            var lstEntity = unitofwork.context.Set<TEntity>().Where(lamada);
            foreach (var entity in lstEntity)
            {
                unitofwork.RegisterDeleted(entity);
            }
            return IsSave? unitofwork.Commit():0;

        }
        #endregion
        #region 修改方法
        /// <summary>
        /// 单项修改
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
       public  int Update(TEntity entity)
        {
            unitofwork.RegisterModified(entity);
            return unitofwork.Commit();
        }
       public  int Update(TEntity entity,bool IsSave)
        {
            unitofwork.RegisterModified(entity);
            return IsSave?unitofwork.Commit():0;
        }
        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="entities">实体集合</param>
        /// <returns></returns>
       public  int Update(IEnumerable<TEntity> entities)
        {
            foreach(var entity in entities)
            {
                unitofwork.RegisterModified(entity);
            }
            return unitofwork.Commit();
        }
       public  int Update(IEnumerable<TEntity> entities,bool IsSave)
        {
            foreach (var entity in entities)
            {
                unitofwork.RegisterModified(entity);
            }
            return IsSave? unitofwork.Commit():0;
        }
        #endregion
        #region 查询方法
        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="PageSize">每页记录数</param>
        /// <param name="PageCount">输出页总数</param>
        /// <param name="expression">查询条件Lamada表达式</param>
        /// <returns></returns>
      public   IEnumerable<TEntity> GetPageList(int pageIndex, int PageSize, out int PageCount, Expression<Func<TEntity, bool>> expression, OrderedParallelQuery<TEntity> Order)
        {
            var entities = unitofwork.context.Set<TEntity>().Where(expression);
            PageCount = entities.Count()%PageSize>=0?entities.Count()/PageSize+1:entities.Count();
           return entities.Skip(pageIndex * PageSize).Take(PageSize).AsEnumerable();
        }
       public TEntity GetEntityBykey(object ID)
        {
          return  unitofwork.context.Set<TEntity>().Find(ID);
        }
        #endregion
    }
}
